package com.titaniume.juc;

import java.util.concurrent.CountDownLatch;

/**
 * @ProjectName: juc
 * @Package: com.titaniume.juc
 * @ClassName: CountDownLatchDemo
 * @Author: huangrenfei
 * @Description: 演示  减少计数    -->6个同学陆续离开教室之后，班长锁门
 * @Date: 2021/11/23 14:47
 * @Version: 1.0
 */
public class CountDownLatchDemo {

    public static void main(String[] args) throws InterruptedException {

        //创建CountDownLatch对象，设置初始值
        CountDownLatch countDownLatch = new CountDownLatch(6);
        //6个同学陆续离开教室之后
        for (int i = 1; i <= 6; i++) {
            new Thread(() -> {
                System.out.println(Thread.currentThread().getName() + "号同学离开教室");
                //减少锁存器的计数，如果计数达到零，释放所有等待的线程。
                //计数  -1
                countDownLatch.countDown();
            }, String.valueOf(i)).start();
        }

        //等待
        countDownLatch.await();

        System.out.println(Thread.currentThread().getName()+" 班长锁门走人了");




    }
}
